package com.examination.controller.utils;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

@WebFilter("/*")
public class SesitiveWordsfilter implements Filter {
    //敏感词汇集合
    private List<String> list = new ArrayList<String>();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

        try{
            //1.加载文件真实路径
            ServletContext servletContext = filterConfig.getServletContext();
            String realPath = servletContext.getRealPath("/WEB-INF/wo.txt");
            //2.读取文件
            BufferedReader br = new BufferedReader(new FileReader(realPath));
            //3.将文件的每一行数据添加到list中
            String line = null;
            while((line = br.readLine()) != null ){
                list.add(line);
            }
            br.close();//关闭字符流
            System.out.println(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {



        System.out.println("和谐词过滤器执行");
//        创建对象，增强getParameter
        HttpServletRequest request = (HttpServletRequest)servletRequest;

        //1获取资源请求路径
        String requestRUI = request.getRequestURI();

//        //2判断是否包含登录相关资源路径，但要注意排除掉 css/js/图片，验证码等资源
//        if (requestRUI.contains("/login.jsp")||requestRUI.contains("/loginServlet")
//                ||requestRUI.contains("/css/")
//                ||requestRUI.contains("/js/")
//                ||requestRUI.contains("/fonts/")
//                ||requestRUI.contains("/img/")
//                ||requestRUI.contains("/jstl/")
//                ||requestRUI.contains("/CheckccodeServlet")){
//            //包含，用户就是想登录。放行
//            filterChain.doFilter(servletRequest,servletResponse);
//        }
        ServletRequest proxy_req =  (ServletRequest) Proxy.newProxyInstance(servletRequest.getClass().getClassLoader(), servletRequest.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                //增强是否是getParamter方法
                if (method.getName().equals("getParameter")) {
//                 增强返回值
//                    1.获取返回值
                    String value =(String) method.invoke(servletRequest,servletResponse);
                    if (value != null ){
                        for (String str : list){
                            if (value.contains((str))){
                                value = value.replaceAll(str,"***");
                            }

                        }
                    }
                    return value;

                }
                return method.invoke(servletRequest,objects);
            }
        });
        //放行
        filterChain.doFilter(proxy_req, servletResponse);
    }

    @Override
    public void destroy() {

    }
}